﻿using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Tasks;
using MovingBlocks.Classes;
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Media.Imaging;

namespace MovingBlocks
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Declarations
        PhotoChooserTask photoChooserTask;
        BitmapImage choosenPhotobmp;
        
        // Constructor
        public MainPage()
        {
            // Component from xaml creation
            InitializeComponent();
            
            // ApplicationBar builder
            BuildLocalizedApplicationBar();

            // Photo picker class calling
            BuildPhotoPicker();
        }


        /* Building a localized ApplicationBar */ 
        private void BuildLocalizedApplicationBar()
        {
            // Set the page's ApplicationBar to a new instance of ApplicationBar.
            ApplicationBar = new ApplicationBar();

            // Create a new button and set the text value to the localized string from AppResources.
            ApplicationBarIconButton appClearImageButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
            appClearImageButton.Text = "Remove";  //AppResources.AppBarButtonText;
            ApplicationBar.Buttons.Add(appClearImageButton);
            appClearImageButton.Click += new EventHandler(clearChosenImage);
        }


        /* Build photoPicker class for picture from library selection */
        private void BuildPhotoPicker()
        {
            photoChooserTask = new PhotoChooserTask();
            photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);
            choosenPhotobmp = new BitmapImage();
        }


        /* Choosig the photo from library */
        private void ChooseFileButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                photoChooserTask.Show();

                OpenedImage.Source = choosenPhotobmp;
                StartButton.IsEnabled = true;
            }
            catch
            {
                throw new NotImplementedException();
            }
            //getMovieFromLibrary();
        }


        /* Puts a picture into bitmap */
        void photoChooserTask_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                choosenPhotobmp.SetSource(e.ChosenPhoto);
            }
        }


        /* Clears image from bar button */
        private void clearChosenImage(object sender, EventArgs e)
        {
            try
            {
                choosenPhotobmp = new BitmapImage();
                OpenedImage.Source = null;
                StartButton.IsEnabled = false;
            }
            catch
            {
                throw new NotImplementedException();
            }
        }


        /* Start the game button click */
        private void StartButton_Click(object sender, RoutedEventArgs e)
        {
            InputArgumentsContext.Current.Image = choosenPhotobmp;
            
            selectSizeFromDropDown();

            NavigationService.Navigate(new Uri("/Game.xaml", UriKind.Relative));
        }


        /* Selecting the size from the playground */
        private void selectSizeFromDropDown()
        {
            switch(SizeSelection.SelectedIndex)
            {
                case 0:
                    {
                        InputArgumentsContext.Current.numberOfCols = 3;
                        InputArgumentsContext.Current.numberOfRows = 3;
                        break;
                    }
                case 1:
                    {
                        InputArgumentsContext.Current.numberOfCols = 4;
                        InputArgumentsContext.Current.numberOfRows = 4;
                        break;
                    }
                case 2:
                    {
                        InputArgumentsContext.Current.numberOfCols = 5;
                        InputArgumentsContext.Current.numberOfRows = 5;
                        break;
                    }
                case 3:
                    {
                        int randomizedValue;
                        Random rand = new Random();
                        randomizedValue = rand.Next(6, 15);

                        InputArgumentsContext.Current.numberOfCols = randomizedValue;
                        InputArgumentsContext.Current.numberOfRows = randomizedValue;
                        break;
                    }
            }
            
        }


/***********************************************    OLD CODE    ************************************************/

        private void getMovieFromLibrary()
        {
            try
            {
                /*FileOpenPicker pickedMovie = new FileOpenPicker();
                pickedMovie.ViewMode = PickerViewMode.Thumbnail;
                pickedMovie.SuggestedStartLocation = PickerLocationId.VideosLibrary;*/
                Debug.WriteLine("End picking!!! :)");
            }
            catch
            {
                Debug.WriteLine("End picking fails!!! :)");
            }
            /*StorageFile file = async pickedMovie.PickMultipleFilesAsync();
            if (file != null)
            {
                // Application now has read/write access to the picked file
                OutputTextBlock.Text = "Picked photo: " + file.Name;
            }
            else
            {
                OutputTextBlock.Text = "Operation cancelled.";
            }*/

        }

        
    }
}